/**
 * 
 */
package edu.tsinghua.mrpvc.device.rfid;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.RFIDDetect;
import edu.tsinghua.mrpvc.device.simulate.RFIDSimulateTopic;



/**
 * @author Tangjian Deng & Wei Feng
 *
 */
public class RFIDDetectImpl implements RFIDDetect,EventHandler{
	String deviceID;
	String detectID;
	boolean valid;
	private ServiceTracker tracker;	
	
	public RFIDDetectImpl(BundleContext context, String deviceID){
		this.deviceID = deviceID;
		this.detectID = null;
		this.valid = true;
		tracker = new ServiceTracker(context,EventAdmin.class.getName(), null);
		tracker.open();	}

	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return deviceID;
	}

	public String getDetectID(){
		return detectID;
	}
	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return valid;
	}

	public void detect(){
		//System.out.print(Calendar.getInstance().getTime().toLocaleString() + " ");
		//System.out.println("RFID: Detect Executed");
		setDetectID("1");
	}
	
	private void generEvent(){
		System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
				" RFID: detected owner and posts Event");
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if ( eventAdmin != null ){
			Map<String, String> eventProper = new Hashtable<String, String>();
			eventProper.put(RFIDDetect.EVENT_PROPER_ID, deviceID);
			eventProper.put(RFIDDetect.EVENT_PROPER_DETECT_ID, detectID);
			eventProper.put(RFIDDetect.EVENT_PROPER_ISVALID, RFIDDetect.Constants.ISVALID_TRUE);
			Event event = new Event(RFIDDetect.EVENT_TOPIC_STATE, eventProper);
			eventAdmin.postEvent(event);
		}	
		
	}

	private void setDetectID(String detectID) {
		// TODO Auto-generated method stub
		this.detectID = detectID;
		if(this.detectID != null){
			generEvent();
		}
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		if(event.getTopic().equals(RFIDDetect.EVENT_TOPIC_ISVALID)){
			if(event.getProperty(RFIDDetect.EVENT_PROPER_ID) != null && event.getProperty(RFIDDetect.EVENT_PROPER_ID).equals(deviceID)){
				generEvent();
			}
			else{
				System.out.println("*****RFID: ID not match*******");
			}
		}
		else if(event.getTopic().equals(RFIDSimulateTopic.RFID_DETECT)){
			detect();
		}
		else if(event.getTopic().equals(RFIDSimulateTopic.RFID_VISIT)){
			visit();
		}
		else if(event.getTopic().equals(RFIDSimulateTopic.RFID_LEAVE)){
			leave();
		}
	}
	
	public void visit(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
					" RFID: find someone is outside and post event");
			Map<String, Object> property = new Hashtable<String, Object>();
			Event event = new Event(RFIDDetect.EVENT_TOPIC_VISITOR, property);
			eventAdmin.postEvent(event);
		}	
	}

	@Override
	public int openDoor() {
		// TODO Auto-generated method stub
		System.out.println(Calendar.getInstance().getTime().toLocaleString() +
				" RFID: openDoor executed");
		return 1;
	}
	
	public void leave(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
					" RFID: detect leaving home and post event");
			Map<String, String> proper = new Hashtable<String, String>();
			Event event = new Event(RFIDDetect.EVENT_TOPIC_LEAVE, proper);
			eventAdmin.postEvent(event);
		}
	}
}
